package dao.impl;

import dao.BaseDao;
import dao.ProductDao;
import entity.Product;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductDaoImpl extends BaseDao implements ProductDao {
    Product product = null;
    @Override
    public List<Product> info() {
        conn = super.getConnection();
        List<Product> list = new ArrayList<>();
        try{
            String sql = "SELECT * FROM `metaverse_product`";

            pstmt = conn.prepareStatement(sql);

            rs = pstmt.executeQuery();

            while (rs.next()){
                product = new Product();
                product.setId(rs.getInt("id"));
                product.setProduct_name(rs.getString("product_name"));
                product.setProduct_price(rs.getDouble("product_price"));
                product.setProduct_desc(rs.getString("product_desc"));
                product.setFlag(rs.getInt("flag"));
                list.add(product);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public int inset(Product product) {
        conn = super.getConnection();
        int i =0;
        try{
            String sql = "INSERT INTO `microblog`.`metaverse_product`( `product_name`, `product_price`, `product_desc`, `flag`) VALUES (?,?,?,?);";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, product.getProduct_name());
            pstmt.setDouble(2,product.getProduct_price());
            pstmt.setString(3,product.getProduct_desc());
            pstmt.setInt(4,product.getFlag());
           i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int delete(int id) {
        int i = 0;
        conn = getConnection();
        try{
            String sql = "delete from metaverse_product where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public int update(Product product) {
        conn = super.getConnection();
        int i = 0;
        try{
            String sql  ="UPDATE `microblog`.`metaverse_product` SET `product_name` = ?, `product_price` = ?, `product_desc` = ?, `flag` = ? WHERE `id` = ?;";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, product.getProduct_name());
            pstmt.setDouble(2,product.getProduct_price());
            pstmt.setString(3, product.getProduct_desc());
            pstmt.setInt(4,product.getFlag());
            pstmt.setInt(5,product.getId());
            i = pstmt.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return i;
    }

    @Override
    public Product select(int id) {
        conn = super.getConnection();
        try{
            String sql = "SELECT * FROM `metaverse_product` where id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1,id);
            rs = pstmt.executeQuery();
            while (rs.next()){
                product = new Product();
                product.setId(rs.getInt("id"));
                product.setProduct_name(rs.getString("product_name"));
                product.setProduct_price(rs.getDouble("product_price"));
                product.setProduct_desc(rs.getString("product_desc"));
                product.setFlag(rs.getInt("flag"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return product;
    }


}
